﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Practices.Unity;
using Pro.Common.IoC;

namespace Pro.IoC.Unity
{
    public class UnityComponentContainer : IEngine
    {
        private readonly IUnityContainer _unityContainer;


        public UnityComponentContainer(IUnityContainer unityContainer)
        {
            this._unityContainer = unityContainer;
        }

        public TComponent Resolve<TComponent>()
        {
            return _unityContainer.Resolve<TComponent>();
        }

        public TComponent Resolve<TComponent>(string componentName)
        {
            return _unityContainer.Resolve<TComponent>(componentName);
        }

        public IEnumerable<TComponent> ResolveAll<TComponent>()
        {
            return _unityContainer.ResolveAll<TComponent>();
        }

        public object Resolve(Type componentType)
        {
            return _unityContainer.ResolveAll(componentType);
        }

        public object Resolve(Type componentType, string componentName)
        {
            return _unityContainer.Resolve(componentType, componentName);
        }

        public IEnumerable<object> ResolveAll(Type componentType)
        {
            return _unityContainer.ResolveAll(componentType);
        }

        public bool TryResolve(Type componentType, out object componentInstance)
        {
            try
            {
                componentInstance = _unityContainer.Resolve(componentType);
                return true;
            }
            catch
            {
                componentInstance = null;
                return false;
            }
        }

        public bool TryResolve(Type componentType, string componentName, out object componentInstance)
        {
            try
            {
                componentInstance = _unityContainer.Resolve(componentType, componentName);
                return true;
            }
            catch
            {
                componentInstance = null;
                return false;
            }
        }

        public bool TryResolve(Type componentType, out IEnumerable<object> componentInstances)
        {
            try
            {
                componentInstances = _unityContainer.ResolveAll(componentType);
                return true;
            }
            catch
            {
                componentInstances = null;
                return false;
            }
        }
    }
}
